<template>
  <el-container>
      <el-header>
        <div class="left">
          <img src="../assets/logo.png" alt="" class="logo">
          <span>后台管理系统</span>  
        </div>
        <el-button type="info" @click="logout">退出</el-button></el-header>
      <el-container>
          <el-aside width="isCollapse ? '64px':'200px'">
            <div class="toggle-button" @click="toggleCollapse" >|||</div>
            <el-menu
      background-color="#E4F5EF"
      text-color="black"
      active-text-color="#42B983"
      :collapse="isCollapse"
      :collapse-transition="false"
      :router="true"
      :default-active="activePath">
      <!-- 一级菜单 -->
      <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id">
        <!-- 一级菜单模板区 -->
        <template slot="title">
          <i :class="iconObj[item.id]"></i>
          <span>{{item.authName}}</span>
        </template>
        <!-- 二级菜单 -->
        <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id"
        @click="saveNavState('/'+subItem.path)">
          <template slot="title">
          <i class="el-icon-menu"></i>
          <span>{{subItem.authName}}</span>
        </template>
        </el-menu-item>
      </el-submenu>
    </el-menu>
          </el-aside>
          <el-main>
            <router-view></router-view>
          </el-main>
      </el-container>
  </el-container>
</template>
<script>
export default {
  data(){
    return{
      menulist:[],
      iconObj:{
        '125':'el-icon-user-solid',
        '103':'el-icon-crop',
        '101':'el-icon-shopping-bag-2',
        '102':'el-icon-tickets',
        '145':'el-icon-data-line',

      },
      isCollapse:false,
      activePath:''
    }
  },
  created(){
    this.getMenuList()
    this.activePath=window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList(){
      const{data:res}=await this.$http.get('menus')
      if(res.meta.status!==200)return this.$message.error(res.meta.msg)
      console.log(res.data);
      this.menulist=res.data
    },
    toggleCollapse(){
      this.isCollapse=!this.isCollapse
    },
    saveNavState(activePath){
      window.sessionStorage.setItem('activePath',activePath)
      this.activePath=activePath
    }
  },
}
</script>
<style lang="less" scoped>
.el-header{
  background-color: #BBE6D6;
  display: flex;
  justify-content: space-between;
  padding-left: 5px;
  color: #fff;
  align-items: center;
  font-size: 24px;
  div{
    display: flex;
    align-items: center;
    span{
      margin-left: 10px;
    }
  }
}
.el-aside{
  background-color: #E4F5EF;
  .el-menu{
    border-right: none;
  }
}
.el-main{
  background-color: #C8EBDF;
}
.el-container{
  height: 100%;
}
.logo {
  height:60px ;
}
.toggle-button{
  background-color: #F7FCFA;
  text-align: center;
  font-size: 10px;
  line-height: 25px;
 letter-spacing: 0.2em;
 height: 25px;
 cursor: pointer;
}
</style>
